<template>
  <Row>
    <i-col>
      <!-- 静态table，内置静态搜索，动态修改，表的值 2018/7/15 zhangQ -->
      <i-table ref="iviewTableRef" :columns="columns" :data="pagination.rows" :height="tableHeight"></i-table>
      <Page :total="pagination.total" :current.sync="pagination.pageNumber" :page-size="pagination.pageSize"
            v-if="page && pagination.total>0"
            style="margin-top: 20px"
            show-total
            show-elevator
            :show-sizer="showSizer"
            :page-size-opts="pageSizeOpts"
            @on-change="pageOnChange"
            @on-page-size-change="pageSizeChange"></Page>
    </i-col>
  </Row>
</template>

<script>
  import _ from 'lodash'

  export default {
    name: 'staticTable',
    data () {
      let dataClone = []
      let dataCloneFilter = []
      let firstLoad = true
      let pagination = {
        rows: [], // 当前行
        pageNumber: 1,
        oldPageNumber: 0,
        total: 0,
        pageSize: this.pageSizeOpts[0],
        oldPageSize: 0
      }
      let datas = { pagination, dataClone, dataCloneFilter, firstLoad }
      return datas
    },
    props: {
      /**
       * TODO(是否分页)
       * @auther zhangQ
       * @date 2018/8/26 18:31
       */
      page: {
        type: Boolean,
        default: false
      },
      /* 是否显示分页条数 */
      showSizer: {
        type: Boolean,
        default: false
      },
      /* 分页条数 */
      pageSizeOpts: {
        type: Array,
        default () {
          return [20, 40, 60, 80]
        }
      },
      /**
       * 过滤函数，每行都执行
       * @param dataClone所有修改对象相当于过滤方法，从新注入数据
       * @return Boolean
       * @auther zhangQ
       * @date 2018/7/16 9:34
       */
      filterMethod: {
        type: Function,
        default () {
          return null
        }
      },
      /**
       * TODO(高度差值)
       * @auther zhangQ
       * @date 2018/8/26 20:04
       */
      hReduce: {
        type: Number,
        default: 0
      },
      columns: {
        type: Array,
        default () {
          return []
        }
      },
      data: {
        type: Array,
        default () {
          return []
        }
      }
    },
    computed: {
      tableHeight () {
        // let wh = this.$store.state.windowHeight > 768 ? this.$store.state.windowHeight : 768
        // if (this.page && this.pagination.total > 0) {
        //   wh = wh - 50
        // }
        return this.hReduce
      }
    },
    watch: {
      data (val, oldval) {
        this.firstLoad = true
        this.dataClone = _.cloneDeep(this.data)
        this.dataCloneFilter = null
        let index = 0
        this.dataClone.forEach((item) => {
          item._dataIndex = index
          index++
        })
        this.readLoad()
      }
    },
    methods: {
      /**
       * TODO(得到iview的Table)
       * @auther zhangQ
       * @date 2018/7/15 22:53
       */
      getIviewTable () {
        return this.$refs['iviewTableRef']
      },
      /**
       * 获得全部数据（不涉及过滤）
       * @auther zhangQ
       * @date 2018/7/16 14:45
       */
      getAllData () {
        // 数据先同步
        this.syncData()
        return _.cloneDeep(this.dataClone)
      },
      /**
       * 获得选中的数据和编辑过的数据（不涉及过滤）
       * @auther zhangQ
       * @date 2018/7/16 14:45
       */
      getCheckedData () {
        // 数据先同步
        this.syncData()
        let resData = []
        let iviewTable = this.getIviewTable()
        _.forIn(iviewTable.objData, (val, key) => {
          if (val._isChecked) {
            resData.push(this.dataClone[val._dataIndex])
          }
        })
        return _.cloneDeep(resData)
      },
      /**
       * 自定义过滤方法
       * @auther zhangQ
       * @date 2018/7/16 9:30
       */
      customFilter () {
        if (typeof this.filterMethod !== 'function') {
          console.error('过滤函数错误！')
          return
        }
        // 获取到最新的数据
        let filterDatas = []
        this.dataClone.forEach((item) => {
          let itemClone = _.cloneDeep(item)
          if (this.filterMethod(itemClone)) {
            filterDatas.push(itemClone)
          }
        })
        this.dataCloneFilter = filterDatas
        this.pagination.pageNumber = 1
        this.readLoad()
      },
      /**
       * 取消自定义过滤
       * @auther zhangQ
       * @date 2018/7/16 9:40
       */
      cancelCustomFilter () {
        this.dataCloneFilter = null
        this.pagination.pageNumber = 1
        this.readLoad()
      },
      /**
       * TODO(同步数据)
       * @auther zhangQ
       * @date 2018/8/26 22:37
       */
      syncData () {
        if (this.firstLoad) {
          this.firstLoad = false
          return
        }
        let iviewTable = this.getIviewTable()
        if (iviewTable.rebuildData.length > 0) {
          iviewTable.rebuildData.forEach((item) => {
            let resItem = _.cloneDeep(item)
            delete resItem._rowKey
            this.dataClone[item._dataIndex] = resItem
          })
        }
      },
      /**
       * 获取数据
       * @param filterDatas:::过滤数据
       * @param pagination:::分页数据
       */
      readLoad () {
        // 每次读取数据的时候，数据要同步
        this.syncData()
        let datas = this.dataClone
        if (this.dataCloneFilter && this.dataCloneFilter.length >= 0) {
          datas = this.dataCloneFilter
        }
        if (this.page) {
          this.staticLoadPage(datas)
        } else {
          this.pagination.rows = datas
        }
      },
      /**
       * TODO(静态数据分页)
       * @auther zhangQ
       * @date 2018/8/26 18:44
       */
      staticLoadPage (datas) {
        if (datas && datas.length > 0) {
          this.pagination.rows = []
          let dataClone = _.cloneDeep(datas)
          // 不排序，排序交给调用者
          // if (param.sortName && param.sortName !== '') {
          //   staticDataClone = _.sortBy(staticDataClone, function (o) {
          //     return o[param.sortName]
          //   })
          //   if (param.sortOrder === 'desc') {
          //     staticDataClone.reverse()
          //   }
          // }
          let start = (this.pagination.pageNumber - 1) * this.pagination.pageSize
          let end = this.pagination.pageNumber * this.pagination.pageSize
          this.pagination.total = dataClone.length
          this.pagination.rows = dataClone.slice(start, end)
        } else {
          this.pagination.rows = []
          if (this.pagination.pageNumber > 1) {
            this.pagination.pageNumber = 1
          }
        }
        this.pagination.oldPageNumber = this.pagination.pageNumber
        this.pagination.oldPageSize = this.pagination.pageSize
      },
      /**
       * TODO(页数发生改变)
       * @auther zhangQ
       * @date 2018/8/26 22:04
       */
      pageOnChange (pageNumber) {
        this.pagination.pageNumber = pageNumber
        this.readLoad()
        this.$emit('pageChangeIndex', pageNumber, this.pagination.pageSize)
      },
      /**
       * TODO(每页大小改变)
       * @auther zhangQ
       * @date 2018/8/26 22:06
       */
      pageSizeChange (pageSize) {
        this.pagination.pageSize = pageSize
        this.pagination.pageNumber = 1
        this.readLoad()
        this.$emit('pageSizeChange', this.pagination.pageNumber, this.pagination.pageSize)
      }
    },
    created () {
    },
    mounted () {
    },
    components: {}
  }
</script>

<style scoped>

</style>
